{include file="public/header,public/nav,public/fixbar" title="我的优惠券-<?php echo request()->pc_info['seo_title']?>"/}
<div class="layui-body">
    <div class="layui-container">
        <div class="layui-col-space15">
            <!-- 菜单栏 -->
            <div class="layui-col-xs12 layui-col-sm4 layui-col-md4 layui-col-lg3">
                <div class="layui-main user-main min-height-90vh">
                    {include file="user/menu"/}
                </div>
            </div>
            <div class="layui-col-xs12 layui-col-sm8 layui-col-md8 layui-col-lg9">
                <div class="layui-main user-main min-height-90vh">
                    <h2>我的优惠券</h2>
                    <div class="layui-tab layui-tab-brief margin-bottom-10">
                        <ul class="layui-tab-title">
                            <li class="layui-this select-type" data-type="10">可用优惠券</li>
                            <li class="select-type" data-type="20">已用优惠券</li>
                            <li class="select-type" data-type="30">失效优惠券</li>
                        </ul>
                    </div>
                    <div class="layui-row">
                        <div class="layui-col-space25 scrollbar height-700" id="coupon-list">
                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer"/}
<script type="text/javascript">
    layui.use(['flow','layer'], function(){
        var layer = layui.layer, flow = layui.flow, $=layui.jquery;
        
        var status = 10;
        $(function(){
            loadList(status);  // 加载列表
        })

        // 根据类型读取
        $(".select-type").on('click', function(){
            status = $(this).data('type');
            $('#coupon-list').html('');
            loadList(status);
        });

        // 分页加载列表
        function loadList(status){
            var limit = 20;//每次请求的条数
            flow.load({
                elem: '#coupon-list' //指定列表容器
                ,scrollElem: '#coupon-list'//滚动条所在元素
                ,end: '—— 已经到底了~ ——'
                ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页的回调
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        data: {'page': page, 'limit':limit, 'coupon_status':status},//请求的页码和每页显示条数
                        url: '/user/coupon',
                        success: function (res) {
                            var lis = [];
                            if(res.code == 0){
                                if (res.data.length > 0) {
                                    
                                    var newsHtml = '';
                                    layui.each(res.data, function(index, item){
                                        var discount_type = '';
                                        if (item.discount_type == 1) {
                                            discount_type = '满减券';
                                        }

                                        var color = 'color: #ff5722;';
                                        var img = '';
                                        if (item.coupon_status == 20) {
                                            color = 'color: #999999;';
                                            img = '<img src="/static/images/used.png">'
                                        } else if (item.coupon_status == 30) {
                                            color = 'color: #999999;';
                                            img = '<img src="/static/images/expired.png">'
                                        }
                                        newsHtml += `<div class="layui-col-xs12 layui-col-md6">
                                                        <div class="coupon-card">
                                                            <div class="card-left" style="${color}">
                                                                <div class="price-number">${item.discount_amount}<span class="price-symbol">元</span></div>
                                                                <div class="coupon-type">${item.discount_type}</div>
                                                            </div>
                                                            <div class="card-right layui-ellip">
                                                                <p class="coupon-title layui-ellip">${item.coupon_name}</p>
                                                                <strong class="card-time">有效期 ${item.valid_begin_time} ~ ${item.valid_end_time}</strong>
                                                                ${img}
                                                            </div>
                                                        </div>
                                                    </div>`;
                                    });
                                    
                                    lis.push(newsHtml);
                                    //执行下一页渲染，第二参数为：满足“加载更多”的条件，即后面仍有分页
                                    //res.total为Ajax返回的总页数，只有当前页小于总页数的情况下，才会继续出现加载更多
                                    next(lis.join(''), page < Math.ceil(res.count/limit));
                                    
                                } else {
                                    $('#coupon-list').html('<div class="layui-empty"><img src="/static/images/nodata/no_coupon.png"><p>暂无优惠券~</p></div>');
                                }
                            } else {
                                layer.msg(res.msg, {icon:2, time: 1500});
                            }
                        }
                        ,error:function(e){
                            layer.closeAll();
                            layer.msg("请求失败："+e.msg, {icon:2, time: 1500});
                        },
                    });
                }
            });
        }
    });

</script>